// 加载Express模块
const express = require('express');

// 加载MySQL模块
const mysql = require('mysql');

// 加载bodyParser模块
const bodyParser = require('body-parser');

// 加载MD5模块
const md5 = require('md5');
//文件上传
const multiparty = require('multiparty')
const fse = require('fs-extra')




// 引入路由器接口
const myRouter = require('./router/mypage');
const lsRouter = require('./router/ls');
const userRouter = require('./router/user');
const myfeedback = require('./router/my_feedback');
const myhealthy = require('./router/my_healthy');
const checkapi = require('./router/checkapi')
// 加载swipe模块
var swipeRouter = require('./router/swipe.js');
// 加载register模块
var registerRouter = require('./router/register.js')

// 引入recommend模块
var recommendRouter = require('./router/recommend.js');
var detailsRouter = require('./router/details.js');

// 加载search模块
var searchRouter = require('./router/search.js');

// 创建服务器对象
const server = express();

// 添加错误处理中间件，拦截所有错误
server.use((err,req,res,next)=>{
  console.log(err);
  res.send({code:500,msg:'服务器端错误'})
})


server.use(bodyParser.urlencoded({
  extended: false
}));
server.use(bodyParser.json());


// 加载CORS模块
const cors = require('cors');

// 使用CORS中间件
server.use(cors({
  origin: ['http://localhost:8080', 'http://127.0.0.1:8080']
}));

//挂载路由
server.use('/mypage', myRouter);
server.use('/ls', lsRouter);
server.use('/user', userRouter);
// 使用swipe模块
server.use('/swipe', swipeRouter)
// 使用register模块
server.use('/',registerRouter)
server.use('/', myfeedback)
server.use('/', myhealthy)
// 使用ckapi模块
server.use('', checkapi);

// 使用search模块
server.use('/search', searchRouter)

// 挂载recommend模块
server.use('/recommend', recommendRouter)
server.use('/details', detailsRouter)


// 指定服务器对象监听的端口号
server.listen(3000, () => {
  console.log('server is running...');
});